home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------------------------
-
- strutil.c
-
- This reusable module contains miscellaneous string handling
- utility routines.
-
- Copyright © 1994-1995, Northwestern University.
-
- ----------------------------------------------------------------------------*/
-
- #include <string.h>
- #include <ctype.h>
-
- #include "def.h"
- #include "strutil.h"
- #include "memutil.h"
-
-
-
- /* This table maps characters together for a case and diacritical
- insensitive comparison, using the Mac 8 bit international character set. */
-
- static unsigned char gTable[256] = {
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
- 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
- 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
- 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
- 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
- 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
- 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
- 0x41, 0x41, 0x43, 0x45, 0x4E, 0x4F, 0x55, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x43, 0x45, 0x45,
- 0x45, 0x45, 0x49, 0x49, 0x49, 0x49, 0x4E, 0x4F, 0x4F, 0x4F, 0x4F, 0x4F, 0x55, 0x55, 0x55, 0x55,
- 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0x4F,
- 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xAE, 0x4F,
- 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0x22, 0x22, 0xC9, 0x20, 0x41, 0x41, 0x4F, 0xAE, 0xAE,
- 0xD0, 0xD1, 0x22, 0x22, 0x27, 0x27, 0xD6, 0xD7, 0x59, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
- 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
- 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
- };
-
-
-
- /*----------------------------------------------------------------------------
- MyStrCmp
-
- Compare two strings, case and diacritical insensitive.
-
- Entry: s1 = pointer to first C-format string.
- s2 = pointer to second C-format string.
-
- Exit: function result =
- -1 if s1 < s2.
- 0 if s1 = s2.
- +1 if s1 > s2.
- ----------------------------------------------------------------------------*/
-
- short MyStrCmp (char *s1, char *s2)
- {
- unsigned char *t = gTable;
- unsigned char *us1 = (unsigned char*)s1;
- unsigned char *us2 = (unsigned char*)s2;
-
- while (t[*us1] == t[*us2]) {
- if (*us1++ == 0) return 0;
- us2++;
- }
- return t[*us1] < t[*us2] ? -1 : +1;
- }
-
-
-
- /*----------------------------------------------------------------------------
- MyStrEqual
-
- Compare two strings, case and diacritical insensitive.
-
- Entry: s1 = pointer to first C-format string.
- s2 = pointer to second C-format string.
-
- Exit: function result = true if strings are equal.
- ----------------------------------------------------------------------------*/
-
- Boolean MyStrEqual (char *s1, char *s2)
- {
- unsigned char *t = gTable;
- unsigned char *us1 = (unsigned char*)s1;
- unsigned char *us2 = (unsigned char*)s2;
-
- while (t[*us1] == t[*us2++])
- if (*us1++ == 0) return true;
- return false;
- }
-
-
-
- /*----------------------------------------------------------------------------
- MyStrNEqual
-
- Compare the first n characters of two strings, case and diacritical
- insensitive.
-
- Entry: s1 = pointer to first string.
- s2 = pointer to second string.
- n = number of characters to compare.
-
- Exit: function result = true if equal.
- ----------------------------------------------------------------------------*/
-
- Boolean MyStrNEqual (char *s1, char *s2, short n)
- {
- unsigned char *t = gTable;
- unsigned char *us1 = (unsigned char *)s1;
- unsigned char *us2 = (unsigned char *)s2;
-
- while (--n >= 0 && t[*us1++] == t[*us2++]) /* do nothing */;
- return n < 0;
- }
-
-
-
- /*----------------------------------------------------------------------------
- MyNSubstringSearch
-
- Do a case and diacritical insensitive substring search.
-
- Entry: s1 = pointer to string to be searched.
- s2 = pointer to search string, C-format.
- s1len = length of s1.
- giveTime = pointer to give time function, or nil if none.
-
- Exit: function result = error code = userCanceledErr if
- giveTime != nil and search canceled by user, else
- noErr.
- *offset = offset in s1 of first occurence of s2, or
- -1 if none found.
- ----------------------------------------------------------------------------*/
-
- OSErr MyNSubstringSearch (char *s1, char *s2, long s1len, long *offset,
- OSErr (*giveTime)(Boolean))
- {
- unsigned char *t = gTable;
- unsigned char *p, *q, *s1Start;
- long n;
- unsigned long tickLongTime;
- Boolean longTime = false;
- OSErr err = noErr;
-
- if (*s2 == 0) {
- *offset = 0;
- return noErr;
- }
- n = s1len - strlen(s2) + 1;
- s1Start = (unsigned char*)s1;
- tickLongTime = TickCount() + 30;
- while (n-- > 0) {
- p = (unsigned char*)s1;
- q = (unsigned char*)s2;
- while (t[*p] == t[*q]) {
- q++;
- if (*q == 0) {
- *offset = (unsigned char*)s1 - s1Start;
- return noErr;
- }
- p++;
- }
- s1++;
- if (giveTime != nil && (n & 0xf) == 0) {
- if (!longTime && TickCount() > tickLongTime) longTime = true;
- if (longTime) {
- err = (*giveTime)(false);
- if (err != noErr) return err;
- }
- }
- }
- *offset = -1;
- return noErr;
- }
-
-
-
- /*----------------------------------------------------------------------------
- MySubstringSearch
-
- Do a case and diacritical insensitive substring search.
-
- Entry: s1 = pointer to string to be searched, C-format.
- s2 = pointer to search string, C-format.
- giveTime = pointer to give time function, or nil if none.
-
- Exit: function result = error code = userCanceledErr if
- giveTime != nil and search canceled by user, else
- noErr.
- *offset = offset in s1 of first occurence of s2, or
- -1 if none found.
- ----------------------------------------------------------------------------*/
-
- OSErr MySubstringSearch (char *s1, char *s2, long *offset,
- OSErr (*giveTime)(Boolean))
- {
- return MyNSubstringSearch (s1, s2, strlen(s1), offset, giveTime);
- }
-
-
-
- /*----------------------------------------------------------------------------
- MyNIsASubstring
-
- Do a case and diacritical insensitive substring search.
-
- Entry: s1 = pointer to string to be searched.
- s2 = pointer to search string, C-format.
- s1len = length of s1.
-
- Exit: function result = true if s1 contains s2.
- ----------------------------------------------------------------------------*/
-
- Boolean MyNIsASubstring (char *s1, char *s2, long s1len)
- {
- long offset;
-
- MyNSubstringSearch (s1, s2, s1len, &offset, nil);
- return offset != -1;
- }
-
-
-
- /*----------------------------------------------------------------------------
- MyIsASubstring
-
- Do a case and diacritical insensitive substring search.
-
- Entry: s1 = pointer to string to be searched, C-format.
- s2 = pointer to search string, C-format.
-
- Exit: function result = true if s1 contains s2.
- ----------------------------------------------------------------------------*/
-
- Boolean MyIsASubstring (char *s1, char *s2)
- {
- long offset;
-
- MyNSubstringSearch (s1, s2, strlen(s1), &offset, nil);
- return offset != -1;
- }
-
-
-
- /*----------------------------------------------------------------------------
- MyIsASubstringHandle
-
- Do a case and diacritical insensitive substring search.
-
- Entry: h = handle to text to be searched.
- s2 = pointer to search string, C-format.
-
- Exit: function result = true if s1 contains s2.
- ----------------------------------------------------------------------------*/
-
- Boolean MyIsASubstringHandle (Handle h, char *s2)
- {
- long len, offset;
-
- len = MyGetHandleSize(h);
- MyNSubstringSearch (*h, s2, len, &offset, nil);
- return offset != -1;
- }
-
-
-
- /*----------------------------------------------------------------------------
- CopyPascalString
-
- Copy a pascal format string.
-
- Entry: to = pointer to destination string.
- from = pointer to source string.
- ----------------------------------------------------------------------------*/
-
- void CopyPascalString (StringPtr to, StringPtr from)
- {
- BlockMoveData(from, to, *from+1);
- }
-
-
-
-
- /*----------------------------------------------------------------------------
- CrackNum
-
- Crack a decimal number from a string after skipping white space.
-
- Entry: *x = pointer to string.
-
- Exit: function result = cracked number.
- *x = pointer to first character following number in string.
- ----------------------------------------------------------------------------*/
-
- long CrackNum (char **x)
- {
- long result = 0;
- char *y;
-
- y = *x;
- while (isLWSP(*y)) y++;
- while (isdigit(*y)) {
- result = 10*result + (*y - '0');
- y++;
- }
- *x = y;
- return result;
- }
-
-
-
- /*----------------------------------------------------------------------------
- GetIndCString
-
- Get a C-format string from an STR# resource.
-
- Entry: rID = resource id of STR# resource.
- index = index of string in STR# resource.
-
- Exit: str = string.
- ----------------------------------------------------------------------------*/
-
- void GetIndCString (char *str, short rID, short index)
- {
- GetIndString((StringPtr)str, rID, index);
- p2cstr((StringPtr)str);
- }
-
-
-
- /*----------------------------------------------------------------------------
- GetCString
-
- Get a C-format string from the STR# 128 resource.
-
- Entry: index of string in STR# 128.
-
- Exit: str = string.
- ----------------------------------------------------------------------------*/
-
- void GetCString (short index, char *str)
- {
- GetIndString((StringPtr)str, 128, index);
- p2cstr((StringPtr)str);
- }
-
-
-
- /*----------------------------------------------------------------------------
- GetPString
-
- Get a P-format string from the STR# 128 resource.
-
- Entry: index of string in STR# 128.
-
- Exit: str = string.
- ----------------------------------------------------------------------------*/
-
- void GetPString (short index, StringPtr str)
- {
- GetIndString(str, 128, index);
- }
-
-
-
- /*----------------------------------------------------------------------------
- GetEquivalentCharacters
-
- Get all characters equivalent to a character under case and diacritical
- insensitive comparison.
-
- Entry: x = character.
-
- Exit: equivs = C-format list of equivalent characters.
- *numEquiv = number of equivalent characters.
- ----------------------------------------------------------------------------*/
-
- void GetEquivalentCharacters (unsigned char x, unsigned char *equivs, short *numEquiv)
- {
- unsigned char y;
- unsigned char *p, *q;
- short z;
-
- y = gTable[x];
- q = equivs;
- for (z = 0, p = gTable; z < 256; z++, p++) {
- if (*p == y) *q++ = z;
- }
- *q = 0;
- *numEquiv = q - equivs;
- }
-